/**
 * Created by zhengjiachao on 2017/4/26.
 */
(function() {
    ADF.actionModel.DeleteModel = function() {
        ADF.actionModel.DeleteModel.superclass.constructor.call(this);
        this.config = {text:"删除",icon:"icon-shanchu"};
        this.id = "delete";
        this.hotkeyInfo = {
            type : "keydown",
            key : "Delete"
        }
    };

    COM.util.defineClass(ADF.actionModel.DeleteModel, AGEF.CommandActionModel, {
        getCommands: function(graphicalViewer) {
            var theSelections = graphicalViewer.getSelection();
            if (theSelections.length == 0) {
                return [];
            }
            if (theSelections.length == 1 && COM.util.isInstanceOf(theSelections[0], ADF.editpart.DiagramEditPart)) {
                return [];
            }
            var commands = new COM.gef.CompoundCommand();
            theSelections.forEach((item) => {
                if(COM.util.isInstanceOf(theSelections[0], ADF.editpart.DiagramEditPart)){
                    return;
                }
                var model = item.getModel();
                if (COM.util.isInstanceOf(model, ADF.model.BaseModel)) {
                    var cmd = COM.gef.Command.createCommand({
                        execute: function() {
                            this.index = COM.util.Array.indexOf(this.parent.getChildren(), this.node);
                            this.parent.removeChild(this.node);
                        },
                        undo: function() {
                            this.parent.addChild(this.node, this.index);
                        }
                    });

                    cmd.parent = model.getParent();
                    cmd.node = model;
                    commands.add(cmd);

                }
            });
            return [commands];
        },
        getEnabledState : function(graphicalViewer){
            var theSelections = graphicalViewer.getSelection();
            if (theSelections.length == 0) {
                return false;
            }
            if (theSelections.length == 1 && COM.util.isInstanceOf(theSelections[0], ADF.editpart.DiagramEditPart)) {
                return false;
            }
            return true;
        }
    });
})();